home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
500 MB Nyheder Direkte fra Internet 2
/
500 MB nyheder direkte fra internet CD 2.iso
/
start
/
data
/
text
/
fun.txt
< prev
next >
Wrap
Text File
|
1994-09-21
|
85KB
|
1,875 lines
ENTER BASIC
To program the computer, you must use a language that the
computer understands. Most computers understand a language called
BASIC, which is a small part of English.
BASIC was invented by two Dartmouth College professors (John
Kemeny and Tom Kurtz) in 1964. Later they improved it. Today,
BASIC consists of words such as PRINT, GO, TO, INPUT, IF, and
THEN.
Here's how to program the computer by using those BASIC words.
Microsoft BASIC
Different computers speak different dialects of BASIC. The most
popular dialect was invented in 1975 by a 19-year-old kid, Bill
Gates. Since he developed software for microcomputers, he called
himself Microsoft and called his BASIC dialect Microsoft BASIC.
Since Microsoft BASIC is so wonderful, all the popular computer
companies paid him to make their computers understand Microsoft
BASIC. That's right: IBM, Apple, Commodore, Tandy, Atari, Texas
Instruments, and hundreds of other computer companies all had to
pay off Bill.
Microsoft BASIC has become so popular that Bill had to hire
hundreds of employees to help him fill all the orders. Microsoft
Incorporated has become a multi-billion-dollar company, and Bill
has become a famous billionaire, the wealthiest person in America
___ he's still just in his 30's!
Over the years, Bill gradually improved Microsoft BASIC. Some
computers use old versions of Microsoft BASIC; other computers
use his latest improvements.
What's in this chapter?
This chapter on BASIC explains
Modern Microsoft BASIC, whose popular commands and functions are
explained on the following pages:
Command What the computer will
do
Page
Similar to
BEEP hum for a quarter of a
second
373
SOUND, PLAY
CIRCLE (100,100),50 draw a circle at
(100,100) with radius 50
372
LINE, PAINT
CLOSE put finishing touches
on the data files
415
OPEN
CLS clear the screen, so
it becomes blank
333
NEW, LOCATE
DATA MEAT,POTATOES use this list of data:
MEAT, POTATOES
350
READ, RESTORE
DATE$="01-24-1996" set the clock/calendar
to 01-24-1996
395
TIME$=
DEFINT A-Z make all the numeric
variables be integers
397
X=
DELETE 30-80 delete lines 30-80
from the program
332
LIST, EDIT
DIM X$(7) make X$ be a list of 7
strings
379
X=
EDIT 30 edit line 30 of the
program
332
LIST, DELETE
END skip the rest of the
program
336
STOP
FIELD 1, 30 AS X$ let file#1's record be
30 bytes and called X$
417
GET, PUT
FILES print the names of all
the disk files
336
SAVE, LOAD
FOR X = 1 TO 100 repeat the lines
underneath, 100 times
355
NEXT
GET 1,7 from file#1, get the
7th record
417
FIELD, PUT
GO TO 10 skip to line 10 of the
program
334
GOSUB
GOSUB 1000 do the subroutine that
begins at line 1000
376
RETURN
IF A$="FINE" THEN PRINT if A$ is ``FINE'',
print a blank line
345
ON N GO TO
INPUT "WHAT NAME";N$ ask ``WHAT NAME?'' and
get answer N$
341
LINE INPUT
INPUT#1, A$ input from file#1 the
value of A$
415
INPUT
KILL "JOE" erase the file JOE
from the disk
337
FILES, NEW
LINE (0,0)-(100,100) draw a line from (0,0)
to (100,100)
372
PLOT, CIRCLE
LINE INPUT "TYPE IT";N$ say ``TYPE IT'' and
grab whole line as input
407
INPUT
LIST print a list of all
the program's lines
331
LLIST, EDIT
LLIST copy all the program's
lines onto paper
333
LIST, PRINT#1
LOAD "JOE" copy the program JOE
from disk to RAM
337
SAVE, RUN
LOCATE 3,7 move to the 3rd line
of screen, 7th position
371
PRINT, CLS
LPRINT "I LOVE YOU" print ``I LOVE YOU''
on paper
333
PRINT, PRINT#1
LSET X$="I LOVE LUCY" make field variable X$
be ``I LOVE LUCY''
417
FIELD, PUT
MID$(A$,2)="OWL" change the middle of
A$ to ``OWL''
394
X=
NEW start a new program
328
RUN, DELETE
NEXT X repeat the lines
above, for the next X
355
FOR
ON ERROR GO TO 1000 if the lines below
cause errors, go to 1000
414
RESUME
ON N GO TO 80,100,20,350 go to 80, 100, 20, or
350, depending on N
413
ON N GOSUB
ON N GOSUB 80,100,20,350 gosub 80, 100, 20, or
350, depending on N
413
ON N GO TO
OPEN "S" FOR OUTPUT AS 1 create a data file
called ``S''; output to it
415
CLOSE
PAINT (100,101) fill in the shape that
surrounds (100,101)
372
LINE, CIRCLE
PLAY "CDG#B-A" play this music: C, D,
G sharp, B flat, A
374
SOUND, BEEP
PLOT (100,100) put a dot at the point
(100,100)
372
LINE, CIRCLE
POKE 7512,14 into memory cell
#7512, put the number 14
414
X=
PRINT 5+2 print the answer to
5+2
326
?
PRINT USING "##.#"; 30/7 print 30/7, rounded to
one decimal place
374
PRINT
PRINT#1, "EAT" print onto file#1 the
word ``EAT''
415
PRINT, LPRINT
PUT 1,50 in file#1, change the
50th record
417
FIELD, GET
RANDOMIZE make random numbers be
unpredictable
388
X=
READ A$ get a string from the
DATA and call it A$
350
DATA, RESTORE
RENAME "JOE" TO "FRED" find the file JOE and
rename it ``FRED''
337
FILES, SAVE
RENUM renumber all the
program's lines, by 10's
367
LIST, EDIT
RESTORE go back to the
beginning of the data
351
READ, DATA
RESUME 30 end the error trap, by
going to line 30
414
ON ERROR GO TO
RETURN return to the main
routine (from subroutine)
376
GOSUB
RUN obey all the program's
instructions now
328
LIST, LOAD
SAVE "JOE" copy the program to
disk and call it ``JOE''
336
FILES, LOAD
SOUND 440,18.2 make a sound of 440
hertz, for 1 second
373
PLAY, BEEP
STOP stop the program and
print a message
336
END
SWAP A,B make A and B swap
values with each other
409
X=
TIME$="13:45:07" set the clock to 7
seconds after 13:45
395
DATE$=
X=47 make X stand for the
number 47
337
INPUT, POKE
? 5+2 print the answer to
5+2
326
PRINT
'THIS PROGRAM IS DUMB note that this program
is dumb
367
PRINT
Function Meaning Value PageSimilar to
ABS(-7) absolute value of -77 386INT, SGN
ASC("A") ASCII code number for A65392CHR$
ATN(1)/D arctangent of 1, in degrees45396TAN
CHR$(65) 65th ASCII character``A''392ASC
COS(60*D) cosine of 60 degrees.5 396SIN, TAN
CVD("RAT NAN~")convert to double-precision.2014209944946413417MKD
$
CVI("ME") convert to integer17741 417MKI$
CVS("NAN~") convert to single-precision.201421417MKS$
DATE$ today's date varies 395TIME$
EOF(1) test whether at end of file#1varies416LOF
EXP(1) e raised to the first power2.718282384LOG, SQR
INPUT$(4) 4 characters that are inputvaries407STICK
INSTR("NEEDED","ED")position of ED in NEEDED3395other INSTR
INSTR(4,"NEEDED","ED")search from the 4th character5395other
INSTR
INT(17.9) turn into an integer17 386ABS, SGN
LEFT$("SMART",2)left 2 characters of SMART``SM''394RIGHT$, MID$
LEN("SMART") length of SMART 5 394RIGHT$, MID$
LOC(1) location of record in file#1varies417LOF
LOF(1) length of file#1, in bytesvaries416EOF
LOG(2.718282) logarithm base e1 385EXP
MID$("SMART",2)begin at the 2nd character``MART''394other MID$
MID$("SMART",2,3)begin at the 2nd, take 3``MAR''394other MID$
MKD$(.2014209944946413)make the double a string``RAT NAN~''417CVD
MKI$(17741) make the integer a string``ME''417CVI
MKS$(.201421) make the single a string``NAN~''417CVS
MOUSE(1) how far mouse to rightvaries408STICK
PEEK(7512) peek at memory cell #7512varies414RND
RIGHT$("SMART",2)rightmost 2 characters``RT''394MID$
RND random decimal varies 391other RND
RND(5) random integer from 1 to 5varies388other RND
SGN(-546) sign of -546 -1 386ABS, INT
SIN(30*D) sine of 30 degrees.5 396COS, TAN
SQR(9) square root of 93 384EXP, LOG
STICK(0) how far joystick to rightvaries408MOUSE
STR$(81.4) turn 81.4 into a string`` 81.4''395VAL
STRING$(5,"B")a string of 5 B's``BBBBB''395other STRING$
STRING$(5,66) 66th ASCII character, 5 times``BBBBB''395other
STRING$
TAN(45*D) tangent of 45 degrees1 396ATN
TIME$ current time of dayvaries395TIMER, DATE$
TIMER # of seconds since midnightvaries395TIME$, DATE$
VAL("72.6") remove the quotation marks72.6395STR$
All popular microcomputers use either Modern Microsoft BASIC or
a slight variation of it.
At the end of this BASIC chapter is an amazing appendix called
``Versions of BASIC''. It explains the versions of BASIC used by
IBM, Commodore, Apple, Tandy, and dozens of other manufacturers,
including yours. It reveals the subtle ways that your computer's
BASIC differs from Modern Microsoft BASIC.
If you don't have a computer yet, ignore the appendix: read
just the main text, which explains Modern Microsoft BASIC.
If you do have access to a computer, read the main text (which
explains Modern Microsoft BASIC); but before trying the examples
on your computer, peek at the appendix to see how your computer's
BASIC differs from Modern Microsoft BASIC. To reach that appendix
easily, put a bookmark now at the following page:
Kind of computer you have Where to put the bookmark
IBM PC or any computer that uses IBM PC softwarepage 422
Apple Mac page 427
Apple 2, 2+, 2e, 2c, 2c+, or 2GSpage 431
Commodore page 438
Tandy's Radio Shack TRS-80 model 4page 443
Tandy's Radio Shack TRS-80 Color Computerpage 445
other computer page 449
If you ever get confused about how to program your computer,
phone me at 617-666-2666. I'll help you, free!
The keyboard
Each year, manufacturers improve their keyboards. I expect that
someday most manufacturers will use the Walter keyboard, which I
invented several years ago. It looks like this:
┌──────┬───────┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─────
─┬──────┐
│Clear │ClrDoc │ ! │ @ │ # │ $ │ % │ ^ │ & │ * │ ( │ ) │ _
│BackWd│DeleWd│
│Help │Escape │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 0 │ -
│Backsp│Delete│
├──────┼───────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬───
─┴┬─────┤
│ \ │ │ │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │ + │ {
│ } │
│LfTab │ Tab │ │ │ │ │ │ │ │ │ │ │ = │ [
│ ] │
├──────┼─────────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──
──┴─────┤
│ ` │ ~ │ A │ S │ D │ F │ G │ H │ J │ K │ L │ : │ " │
│
│Functn│ Control │ │ │ │ │ │ │ │ │ │ ; │ ' │
Enter │
├──────┼──────┬───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴──
─┬──────┤
│Boot │PrtDoc│ │ Z │ X │ C │ V │ B │ N │ M │ < │ > │ ? │
│ Top │
│Break │Print │Shift│ │ │ │ │ │ │ │ , │ . │ /
│Shift│ │
├──────┼──────┼─────┴┬──┴───┼───┴───┴───┴───┴───┼───┴─┬─┴───┼────
─┼──────┤
│ │ │ │ │ │Copy
│LfMar│RtMar│Bottom│
│Pause │ Echo │ Caps │Insert│ Space │Move │ │ ■
│ │
└──────┴──────┴──────┴──────┴───────────────────┴─────┴─────┴────
─┴──────┘
Now I'm going to explain how to use the most important keys.
But don't touch the keys on your keyboard until you've read the
next section (which is called ``Get Started'') and the ``Versions
of BASIC'' appendix (which reveals how your keyboard differs from
the Walter keyboard).
Numbers Look at the Walter keyboard. The top row of keys
contains the numbers.
Don't confuse 1 with I; the number 1 is in the top row. Don't
confuse zero with the letter O; the number zero is in the top
row.
(On older keyboards, the zero has a slash through it, like
this: O. That's called a slashed zero or a Swedish zero.)
The SHIFT key One of the keys looks like this:
┌───┐
│ $ │
│ 4 │
└───┘
If you press that $4 key, you'll type a 4. Here's how to type a
dollar sign instead: hold down the SHIFT key; and while you keep
holding down the SHIFT key, tap the $4 key. Here's the general
rule: if a key has two symbols on it, and you want to type the
top symbol, hold down the SHIFT key.
About the SHIFT key, beginners often make two boo-boos. The
first boo-boo is forgetting to press it. For example, suppose you
want to type a dollar sign, and you press the $4 key but forget
to hold down the SHIFT key; then you'll be typing a 4 instead.
The other boo-boo is trying to hit the SHIFT key and another
key at exactly the same time. You can't do it; it's impossible;
you'll wind up hitting one key before the other. The trick is to
hold down the SHIFT key first; and while you keep holding it
down, give the other key a light tap.
Beginners often make those boo-boos. When they realize their
mistakes, they say ``Oh, shit!'' That's why programmers call it
the ``shit'' key.
The keyboard contains two SHIFT keys. They serve the same
purpose as each other. Use whichever SHIFT key makes your fingers
feel more comfortable.
The CAPS key If you tap the CAPS key, the computer will
automatically capitalize all the letters you type. For example,
if you tap the CAPS key and then press the M and E keys, the
screen will show a capitalized ME. The computer will continue to
capitalize all the letters, until you tell the computer to stop
capitalizing.
To make the computer stop capitalizing, just tap the CAPS key
again. After you've told the computer to stop capitalizing,
pressing the M and E keys make the screen show a small me ___
unless you simultaneously hold down the SHIFT key.
The BACKSPACE key The BACKSPACE key says BACKSP on it. If you
make a typing mistake, erase the mistake by pressing the
BACKSPACE key, which makes the computer erase the last character
you typed. To erase two characters, press the BACKSPACE key
twice.
(On older keyboards, the BACKSPACE key has a left-arrow on it,
like this: . To find out about your keyboard's BACKSPACE key,
check the ``Versions of BASIC'' appendix.)
The ENTER key The most important key is the ENTER key. When
using BASIC, press the ENTER key at the end of every line you
type. The ENTER key takes the line you've typed and enters that
line into the computer, so that the computer reads the line.
The computer ignores what you type, until you press the ENTER
key. If you forget to press the ENTER key at the end of the line,
the computer doesn't read what you typed, and so the computer
doesn't do anything, and you wonder why the computer seems
broken.
(On older keyboards, the ENTER key is called the RETURN key.)
Get started
Make sure the various parts of your computer system are
correctly attached to each other. Then turn on the power. If your
computer is modern (and uses Microsoft BASIC), it will say:
OK
(Older computers don't say OK. Instead, they say READY or print
a bracket. For further details about your computer, read the
``Versions of BASIC'' appendix.)
Be bold In science fiction, computers blow up; in real life,
they never do. No matter what keys you press, no matter what
commands you type, you won't hurt the computer. The computer is
invincible! So go ahead and experiment. If it doesn't like what
you type, it will gripe at you, but so what?
Troubles When you try using the computer, you'll have trouble
___ because you're making a mistake, or the computer is broken,
or the computer is weird and works differently from the majority
of computers discussed in this book. (Each computer has its own
``personality'', its own quirks.)
Whenever you have trouble, laugh about it, and say, ``Oh, boy!
Here we go again!'' (If you're Jewish, you can say all that more
briefly, in one word: ``Oy!'') Then get some help.
Get help For further help with your computer, read the
``Versions of BASIC'' appendix. For even more help, read the
beginner's manual that came with your computer, or ask the genie
who gave you the computer (your salesperson or parent or boss or
teacher or friend).
If you're sitting near computers in your office, school, or
home, and other people are nearby, ask them for help. They'll
gladly answer your questions because they like to show off and
because the way they got to know the answers was by asking.
Computer folks like to explain computers, just as priests like
to explain religion. Remember: you're joining a cult! Even if you
don't truly believe in ``the power and glory of computers'', at
least you'll have a few moments of weird fun. So play along with
the weird computer people, boost their egos, and they'll help you
get through your initiation rite. Above all, assert yourself, and
ask questions. ``Shy guys finish last.''
When dealing with the computer and the people who surround it,
be friendly but assertive. To make sure you get your money's
worth from a computer course, ask your teachers and coworkers
questions, questions, questions! If you're using a computer that
you won, get help from the person who gave it to you.
Your town probably has a computer club. (To find out, ask the
local schools and computer stores.) Join the club, and tell the
members you'd like help with your computer. Probably some
computer hobbyist will help you.
And remember ___ you can call me anytime at 617-666-2666, and
I'll help you, free!
MATH
Let's try to make the computer print the answer to 5+2. (And
let's hope the computer says 7.)
Type this:
PRINT 5+2
Type that carefully! Here's how. . . .
First, type the word PRINT, by pressing the P key, then the R
key, then I, then N, then T. (Since most computers don't care
about capitalization, don't bother pressing the SHIFT key.)
After typing the word PRINT, type a blank space (by pressing
the SPACE bar). Then press the 5 key. Then type a plus sign, but
be careful: on most keyboards, the plus sign is on the top part
of a key; so to type the plus sign, you must hold down the SHIFT
key. Then type the 2.
When you've finished typing that line, press the ENTER key,
which makes the computer read what you've typed.
Then the computer will print the answer:
7
If your computer prints a wrong answer instead or says ERROR,
you made a typing mistake. Find your mistake (by looking at the
screen), and then try again to type:
PRINT 5+2
Underneath the answer, the computer will print the word OK (or
some old-fashioned word, such as READY). That means it's okay for
you to feed the computer the next problem; the computer's ready
for another problem.
If you want to subtract 3 from 7, type this:
PRINT 7-3
(To type the minus sign, you do not press the SHIFT key.) When
you've finished typing that line, remember to press the ENTER
key, which makes the computer read that line and print the
answer. The computer will print:
4
You can use decimal points and negative numbers. For example,
if you type this ___
PRINT -26.3+1
the computer will print:
-25.3
Multiplication
To multiply, use an asterisk. To multiply 2 by 6, type this:
PRINT 2*6
The computer will print:
12
Division
To divide, use a slash. So to divide 8 by 4, type this:
PRINT 8/4
The computer will print:
2
Question mark
If you're too lazy to type the word PRINT, type a question mark
instead. For example, instead of typing ___
PRINT 5+2
you can type:
? 5+2
The question mark is an
abbreviation. It stands for the word PRINT.
If you wish, think of
the question mark as standing for the word ``What's''. For
example, if you want to ask the computer ``What's 5+2'', type
this:
? 5+2
Huge & tiny numbers
Do not put commas in
large numbers. To write four million, do not write 4,000,000;
instead, write 4000000.
If the computer's answer
is huge (more than a million) or tiny (less than .01), the
computer might print the answer strangely. Here's how. . . .
E notation The computer
might print the letter E, which means ``move the decimal point''.
For example, suppose the
computer says the answer to a problem is:
8.51673E+12
The E means, ``move the decimal point''. The plus sign means,
``towards the right''. Altogether, the E+12 means, ``move the
decimal point towards the right, 12 places.'' So look at 8.51673,
and move the decimal point towards the right, 12 places; you get
8516730000000.
So when the computer
says the answer is 8.51673E+12, the computer really means the
answer is 8516730000000, approximately. The exact answer might be
8516730000000.2 or 8516730000000.79 or some similar number, but
the computer prints just an approximation.
Suppose your computer
says the answer to a problem is:
9.26E-04
After the E, the minus sign means, ``towards the left''. So look
at 9.26, and move the decimal point towards the left, 4 places.
You get:
.000926
So when the computer says the answer is 9.26E-04, the computer
really means the answer is:
.000926
You'll rarely see E
notation: the computer uses it only if an answer is huge (many
millions) or tiny (tinier than .01). But when the computer does
use E notation, remember to move the decimal point!
The highest number The
highest number the computer can handle is about 1E38, which is 1
followed by 38 zeros, like this:
100000000000000000000000000000000000000
If you try to go much higher, the computer will say:
OVERFLOW ERROR
The tiniest decimal The
tiniest decimal the computer can handle is about 1E-38, which is
a decimal point followed by 38 digits, 37 of which are zeros,
like this:
.00000000000000000000000000000000000001
If you try to go tinier, the computer will have an ``underflow
error'' and will ``fake'' the answer: it will give zero instead
of the correct answer.
Order of operations
What does ``2 plus 3 times 4'' mean? The answer depends on who
you ask.
To a clerk, it means ``start with 2 plus 3, then multiply by
4''; that makes 5 times 4, which is 20. But to a scientist, ``2
plus 3 times 4'' means something different: it means ``2 plus
three fours'', which is 2+4+4+4, which is 14.
Since computers were invented by scientists, computers think
like scientists. If you type ___
PRINT 2+3*4
the computer will think you mean ``2 plus three fours'', so it
will do 2+4+4+4 and print this answer:
14
The computer will not print the clerk's answer, which is 20. So
if you're a clerk, tough luck!
Scientists and computers follow this rule: do multiplication
and division before addition and subtraction. So if you type ___
PRINT 2+3*4
the computer begins by hunting for multiplication and division.
When it finds the multiplication sign between the 3 and the 4, it
multiplies 3 by 4 and gets 12, like this:
PRINT 2+3*4
12
So the problem becomes 2+12, which is 14, which the computer
prints.
For another example, suppose you type:
PRINT 10-2*3+72/9*5
The computer begins by doing all the multiplications and
divisions. So it does 2*3 (which is 6) and does 72/9*5 (which is
8*5, which is 40), like this:
PRINT 10-2*3+72/9*5
6 40
So the problem becomes 10-6+40, which is 44, which is the answer
the computer prints.
Parentheses You can use parentheses the same way as in
algebra. For example, if you type ___
PRINT 5-(1+1)
the computer will compute 5-2 and print:
3
You can put parentheses inside parentheses. If you type ___
PRINT 10-(5-(1+1))
the computer will compute 10-(5-2), which is 10-3, and will
print:
7
STRINGS
Let's make the computer
fall in love. Let's make it say, ``I love you''.
Type this:
PRINT "I LOVE YOU"
Type it carefully. Begin
by typing the word PRINT. (If you're lazy, type a question mark
instead.) Then press the SPACE bar. Then type a quotation mark,
but be careful: on most keyboards, the quotation mark is on the
top part of a key; so to type the quotation mark, you must hold
down the SHIFT key. Then type I LOVE YOU. Then type another
quotation mark.
At the end of the line,
press the ENTER key. The computer will obey your command; it will
print:
I LOVE YOU
You can change the
computer's personality. For example, if you give this command ___
PRINT "I HATE YOU"
the computer will reply:
I HATE YOU
Notice that to make the
computer print a message, you must put the message between
quotation marks. The quotation marks make the computer copy the
message without worrying about what the message means. For
example, if you misspell ``I love you'', and type ___
PRINT "AIEEE LUF YA"
the computer will still copy the message (without worrying about
what it means); the computer will print:
AIEEE LUF YA
Jargon
The word ``JOY''
consists of 3 characters: J and O and Y. Programmers say that the
word ``JOY'' is a string of 3 characters.
A string is any
collection of characters, such as ``JOY'' or ``I LOVE YOU'' or
``AIEEE LUF YA'' or ``76 TROMBONES'' or ``GO AWAY!!!'' or ``XYPW
EXR///746''. The computer will print whatever string you wish,
but remember to put the string in quotation marks.
Strings versus numbers
The computer can handle
two types of expressions: strings and numbers. Put strings (such
as ``JOY'' and ``I LOVE YOU'') in quotation marks. Numbers (such
as 2+2) do not go in quotation marks.
Accidents
Suppose you accidentally put the number 2+2 in quotation marks,
like this:
PRINT "2+2"
The quotation marks make the computer think ``2+2'' is a string
instead of a number. Since the computer thinks ``2+2'' is a
string, it copies the string without analyzing what it means; the
computer will print:
2+2
It will not print 4.
Suppose you accidentally forget to put the string ``I LOVE
YOU'' in quotation marks, and type this instead:
PRINT I LOVE YOU
Since you forgot the quotation marks, the computer thinks the I
LOVE YOU is a weird number instead of a string; but the computer
doesn't know what kind of weird number it is since the computer
doesn't know the meaning of love. Whenever the computer is
confused, it either gripes at you or prints a zero. In this
particular example, it will print a zero, like this:
0
So if you incorrectly tell the computer to say it loves you, it
will say zero.
Long commands on small screens
Try typing this command:
PRINT "I LIKE TO EAT AND DRINK AND GAZE INTO YOUR EYES AS WE
DREAM ABOUT LOVE"
That command's long. It won't fit on a single line of your
screen if your screen's small; but go ahead and type the command
anyway. When you reach the screen's right edge, the computer
automatically moves you to the line below so you can continue
typing. Do not press the ENTER key, until you've typed the entire
command.
After typing the entire command, press the ENTER key. The
computer will print:
I LIKE TO EAT AND DRINK AND GAZE INTO YOUR EYES AS WE DREAM ABOUT
LOVE
EASY PROGRAMS
Now that
you've had fun experimenting with the word PRINT, you're ready
for the big-time; you're ready to program the computer!
Three steps
Programming
the computer consists of three steps.
Step 1: type
the word NEW That tells the computer you're going to invent a new
program.
After you
type the word NEW, press the ENTER key. The computer will say OK.
(If your computer is old-fashioned, it will say some other word
instead, such as READY.)
Step 2: type
your program A program is a list of numbered commands. For
example, suppose you want the computer to say:
I LOVE YOU
YOU TURNED ME ON
LET'S GET MARRIED
Type this program:
1 PRINT "I LOVE YOU"
2 PRINT "YOU TURNED ME ON"
3 PRINT "LET'S GET MARRIED"
Every line
of the program must be numbered: you must type those numbers.
Every line of the program must say PRINT; you must type the word
PRINT. (Or, if you're too lazy to type the word PRINT, you can
type a question mark instead.) Every line of that program must
include quotation marks; you must type the quotation marks.
At the end
of each line, press the ENTER key.
Step 3: type
the word RUN That tells the computer to look at the program
you've written, and run through it.
After you've
typed the word RUN, press the ENTER key.
The computer
will print everything you requested; it will print:
I LOVE YOU
YOU TURNED ME ON
LET'S GET MARRIED
Then the
computer will say OK.
Review
Let's review that procedure. You type:
NEW
1 PRINT "I LOVE YOU"
2 PRINT "YOU TURNED ME ON"
3 PRINT "LET'S GET MARRIED"
RUN
The numbered instructions (1, 2, and 3) are called the program.
Above the program, you type the word NEW; below the program, you
type the word RUN. The words NEW and RUN are not numbered.
After you've done all that correctly (and pressed the ENTER key
after the word RUN), the computer will obey the program and print
what you requested.
Each line of the program must be numbered. Putting a number at
the beginning of a line makes the line become part of your
program; the computer won't obey the line until the program is
RUN. Here's the rule: a number in front of the line tells the
computer to wait until you type the word RUN.
Another example
For another example, try typing this:
NEW
1 PRINT "I LONG"
2 PRINT 2+2
3 PRINT "U"
RUN
At the end of each line, remember to press the ENTER key. When
you press the ENTER key after the word RUN, the computer obeys
the program.
The first line makes the computer print ``I LONG''. The second
line makes the computer add 2 and 2, so the computer prints the
number 4. The third line makes the computer print the letter U.
Altogether, the computer prints:
I LONG
4
U
Yes, the computer says it longs for you!
In that example, the computer indents the 4, so that the 4 has
a blank space before it. That's because of this rule: the
computer automatically puts a blank space before every positive
number.
After the computer's finished running that program, if you type
the word RUN again the computer will print again:
I LONG
4
U
A program's a list of numbered instructions. If the program you
typed isn't on the screen anymore, and you'd like to see that
list of numbered instructions again, type the word LIST. The
computer will say:
1 PRINT "I LONG"
2 PRINT 2+2
3 PRINT "U"
Poem
Suppose you want to make
multiple copies of this poem:
SPRING HAS SPRUNG.
THE GRASS HAS RIZ.
I WONDER WHERE
THE FLOWERS IS.
First, turn the poem
into a program, by typing:
NEW
1 PRINT "SPRING HAS SPRUNG."
2 PRINT "THE GRASS HAS RIZ."
3 PRINT "I WONDER WHERE"
4 PRINT "THE FLOWERS IS."
Then type the word RUN (and press the ENTER key). The computer
will print the poem.
If you type the word RUN
again, the computer will print the poem again. Each time you type
the word RUN, the computer will print the poem.
To print many copies of
the poem, just type the word RUN many times, like this. . . .
You type: RUN
Computer types: SPRING HAS
SPRUNG.
THE GRASS HAS
RIZ.
I WONDER WHERE
THE FLOWERS IS.
OK
You type: RUN
Computer types: SPRING HAS
SPRUNG.
THE GRASS HAS
RIZ.
I WONDER WHERE
THE FLOWERS IS.
OK
You type: RUN
Computer types: SPRING HAS
SPRUNG.
etc.
If you hate poetry and
all other forms of art, cheer up: by using the same technique,
you can make the computer type many copies of your favorite
flubbed-up business letter or your favorite obnoxious ad.
Colons
You can put several
statements on the same line, like this:
1 PRINT "I THINK": PRINT "OF FLEAS": PRINT "AND SNEEZE"
The computer will PRINT
``I THINK''. Then the computer will PRINT ``OF FLEAS''. Then the
computer will PRINT ``AND SNEEZE''. Altogether, the computer will
print:
I THINK
OF FLEAS
AND SNEEZE
Become an expert
To invent your own program and make the computer handle it, go
through the three steps again: type the word NEW, then type your
program, then type the word RUN. Remember to press the ENTER key
at the end of each line, and remember that the commands NEW and
RUN are not numbered.
Those three steps (NEW then program then RUN) are all you have
to know about programming! C'mon, write some programs! It's easy!
Try it. You'll have lots of fun!
A person who writes a program is called a programmer.
Congratulations: you're a programmer!
Write several programs like the ones I've shown you already.
Then you can put on your résumé that you have ``a wide variety of
programming experience'', and you can talk your way into a
programming job!
The rest of this chapter explains how to become a good
programmer.
Programming the computer is like driving a car: the only way to
become an expert is to put your hands on that mean machine and
try it yourself.
If you have access to a computer, put this book next to the
computer's keyboard. At the end of each paragraph, type the
examples and look, look, see the computer run. Invent your own
variations: try typing different numbers and strings. Invent your
own programs: make the computer print your name or a poem; make
it solve problems from your other courses and the rest of your
life. The computer's a fantastic toy. Play with it.
If you're a student, don't wait for your instructor to give
lectures and assign homework. Act now. You'll learn more from
handling the computer than from the lectures or readings.
Experience counts.
Let me tell you the story of Charlie. . . .
At Wesleyan University's computer center, one of the directors
was having trouble making the computer print the university's
payroll. He asked me for help, but I said I didn't know either. I
saw a little kid sitting at one of the keyboards. ``Hey,
Charlie,'' I called to him, ``we're having trouble getting the
payroll out.''
Little Charlie came over and typed some stuff on our keyboard.
``The payroll will be out in a minute,'' he said gleefully.
Charlie was just in seventh grade. He'd never taken a computer
course; his school didn't offer one. But by spending the whole
summer just ``hanging around'' our computer, he knew it better
than we.
Be like Charlie. Hang around your computer. Communicate with it
every day. At first, that will be even harder than talking with a
cat or a tree, because the computer belongs to a different
species, a different kingdom; but keep trying. Get to know it as
well as you know your best friend.
When dealing with the computer and the people who surround it,
be friendly but also assertive. To make sure you get your money's
worth from a computer course, ask your teacher, classmates, lab
assistants, and other programmers questions, questions,
questions!
If you're taking a French course, you might find French
difficult; and if you're taking a computer course, you might find
computers difficult also. But even a stupid three-year-old French
kid can speak French, and even kindergarten kids can program the
computer. They've got just one advantage over you: practice!
CORRECTING ERRORS
If you make an error,
you can correct it in three ways. . . .
If you notice the error
before you press the ENTER key, correct the error by using the
BACKSPACE key.
If you mess up the whole
program, rewrite it, by typing the word NEW and then starting
from the beginning again.
If you mess up a line,
retype it underneath. For example, suppose you type ___
1 PRIMT "I LOVE YOU"
and then you notice that you misspelled the word PRINT. Just
retype the line, so your screen looks like this:
1 PRIMT "I LOVE YOU"
1 PRINT "I LOVE YOU"
For another example,
suppose you type this tongue-twister ___
1 PRINT "SHE SELLS"
2 PRINT "SEE SHELLS"
3 PRINT "BY THE SEASHORE"
and then you notice that in line 2, the SEE ought to be SEA.
Underneath the program, retype that line, so your screen looks
like this:
1 PRINT "SHE SELLS"
2 PRINT "SEE SHELLS"
3 PRINT "BY THE SEASHORE"
2 PRINT "SEA SHELLS"
When you type RUN, the computer will run the corrected version
and print:
SHE SELLS
SEA SHELLS
BY THE SEASHORE
When you type LIST, the computer will list the corrected program;
the computer will say:
1 PRINT "SHE SELLS"
2 PRINT "SEA SHELLS"
3 PRINT "BY THE SEASHORE"
What if the computer gripes?
If the computer ever
gripes at you, don't worry: just correct your error.
For example, suppose you
type ___
1 PRIMT "ROSES ARE RED"
2 PRINT "CABBAGE IS GREEN"
3 PRINT "MY FACE IS FUNNY"
4 PRINT "BUT YOURS IS A SCREAM"
and you don't notice that you misspelled PRINT in line 1.
If you type RUN, the
computer will try to run the program but will be confused by line
1, so it will gripe at you. It will say:
SYNTAX ERROR IN 1
That means the computer hasn't the faintest idea of what you're
talking about in line 1.
Your next step is to
correct the error. To do that, just retype line 1 correctly, and
then type the word RUN again.
So altogether, the conversation looks like this. . . .
You type this, but it's wrong:1 PRIMT "ROSES ARE RED"
2 PRINT "CABBAGE IS GREEN"
3 PRINT "MY FACE IS FUNNY"
4 PRINT "BUT YOURS IS A SCREAM"
RUN
The computer gripes at you:SYNTAX ERROR IN 1
You type this to correct error:1 PRINT "ROSES ARE RED"
RUN
The computer recites the poem:ROSES ARE RED
CABBAGE IS GREEN
MY FACE IS FUNNY
BUT YOURS IS A SCREAM
Common bloopers
If you're like most beginners, you'll make these mistakes soon
___ if you haven't made them already!
You type the letter O instead of zero, or type zero instead of
the letter O. Your typing looks correct to you, but the computer
gripes.
You type a command (such as RUN) but forget to press the ENTER
key afterwards. The computer keeps waiting for you to press it.
Since you don't realize the computer's waiting for you, and since
the computer isn't replying, you think the computer broke.
You tell the computer to PRINT a message, but you forget to put
the message in quotation marks. So the computer gripes or prints
a zero instead.
You start typing a new program but forget to type the word NEW.
So when you type RUN, the computer runs a mishmash of the new
program with the previous program and reprints some messages from
the previous program.
Rearranging your program
You don't have to type your program in order.
For example, suppose you type:
4 PRINT "TOES"
2 PRINT "TOAD"
3 PRINT "LICKS"
1 PRINT "MY"
In its mind, the computer automatically rearranges the program,
so the numbers are in increasing order, like this:
1 PRINT "MY"
2 PRINT "TOAD"
3 PRINT "LICKS"
4 PRINT "TOES"
If you'd like to peek inside the computer's mind and see what
program the computer's thinking of, type the word LIST, like
this. . . .
You type this program: 4 PRINT "TOES"
2 PRINT "TOAD"
3 PRINT "LICKS"
1 PRINT "MY"
You type this command: LIST
Computer types the program in increasing order:1 PRINT "MY"
2 PRINT "TOAD"
3 PRINT "LICKS"
4 PRINT "TOES"
If you type RUN, the computer will print:
MY
TOAD
LICKS
TOES
Whenever you're confused, type the word LIST. Then
the computer will tell you what's in its mind; and what's in its
mind might surprise you!
Fancy example Type this
example and see what happens:
NEW
10 PRINT "MY"
90 PRINT "TRUCK"
32 PRINT "SNEEZE"
50 PRINT "TOE"
32 PRINT "NEED A"
70
80 PRINT "HAT"
80
20 PRINT "SORE FEET"
In its mind, the
computer rearranges the program to put the numbers in increasing
order.
The lowest number is 10,
which prints ``MY''. Next comes line 20, which prints ``SORE
FEET''.
Next comes line 32.
Since you typed line 32 twice, the computer assumes you didn't
like the first version and want the second version instead, so
line 32 prints ``NEED A''.
Next comes line 40,
which prints ``TOE''.
Next comes line 70.
Since line 70 is blank, the computer ignores it.
Next comes line 80.
Since you typed line 80 twice, the computer assumes you didn't
like the first version and want the second version instead, which
is blank. Since it's blank, the computer ignores it. So the
computer ignores line 80 altogether!
Finally comes line 90,
which prints ``TRUCK''.
If you type LIST, the
computer will show you the result of all that reasoning. The
computer will type:
10 PRINT "MY"
20 PRINT "SORE FEET"
32 PRINT "NEED A"
50 PRINT "TOE"
90 PRINT "TRUCK"
If you type RUN, the
computer will print:
MY
SORE FEET
NEED A
TOE
TRUCK
If you think about that
example, you'll notice two things:
To revise a line, just retype it.
To erase line 80, just type ___
80
with nothing else on the line.
NEW versus RUN
The word NEW erases your
program from the computer's mind, to make way for a new one.
But the word RUN does
not erase the program. After the run, you can continue inserting,
deleting, and revising lines.
Numbering by tens
If your first line is
numbered 1, and your second line is numbered 2, you can't squeeze
a line between them, since decimals aren't allowed. So expert
programmers number their lines 10, 20, 30, 40, . . . instead of
1, 2, 3, . . .
That way, if your first
line is numbered 10 and your second line is numbered 20, and you
want to insert an extra line between them, you can call it 15. If
you later want to insert an extra line between 10 and 15, you can
call it 12. If you want to insert another line between 10 and 12,
you can call it 11.
Ranges of lines
If your program's too long to fit on your screen, tell the
computer to list part of your program.
For example, to list just line 30, type this:
LIST 30
To list lines 30 through 80, type this:
LIST 30-80
To list from line 30 to the end of your program, type this:
LIST 30-
To list from the beginning of your program up to line 80, type
this:
LIST -80
To delete lines 30 through 80, type this:
DELETE 30-80
That works on most computers. Some computers are different; check
the ``Versions of BASIC'' appendix.
Arrow keys
To correct a mistake in your program quickly, try using the
arrow keys. Here's how.
See the error Make sure the line you want to correct is on the
screen. If it's not on the screen yet, put it onto the screen by
using the word LIST or EDIT.
For example, to put line 30 onto the screen, type ___
LIST 30
or type:
EDIT 30
Move to the error On the keyboard, you'll see a key that shows
an arrow pointing to the left; it's called the left-arrow key.
You'll also see a key that shows an arrow pointing to the right;
it's called the right-arrow key. You'll also see an up-arrow key
and a down-arrow key.
On the computer's screen, you'll see a little blinking
rectangle, called the cursor. Move it to the part of the line you
want to correct, by pressing the arrow keys. (To move the cursor
to the left, press the left-arrow key; to move the cursor to the
right, press the right-arrow key; to move the cursor up, press
the up-arrow key; to move the cursor down, press the down-arrow
key.)
If you typed the word LIST, you must press the up-arrow key to
move the cursor up to the line you want to correct. If you typed
the word EDIT instead, you don't need to press the up-arrow key;
the word EDIT puts the cursor on the correct line automatically.
Correct the error When you've moved the cursor to the part of
the line you want to correct, make your corrections.
To delete a character, move the cursor to that character, then
press the DELETE key. To replace a character, move the cursor to
that character, then type the new character you want instead. To
insert extra characters in the middle of the line, move the
cursor where you want the extra characters to begin, press the
INSERT key, then type the extra characters.
When you've finished correcting the line, press the ENTER key,
which tells the computer to take the corrections seriously.
Final comments After pressing the ENTER key, if you want to
move the cursor to the bottom of the screen, press the down-arrow
key several times.
That method of editing works on most computers. To find out how
to edit easily on your computer, read the appendix.
TRICKY PRINTING
Printing can be tricky!
Here are the tricks. . . .
Semicolons
Run this program:
10 PRINT "FAT";
20 PRINT "HER"
Line 10 makes the
computer print FAT; and line 10 ends with a semicolon. The
semicolon makes the computer print the next item on the same
line; so the computer will print HER on the same line, like this:
FATHER
This command shows what
happens to an evil king on a boat:
PRINT "SIN";"KING"
The computer will print SIN, and will print KING on the same
line, like this:
SINKING
Blank lines
Life consists sometimes
of joy, sometimes of sorrow, and sometimes of a numb emptiness.
To express those feelings, run this program:
10 PRINT "JOY"
20 PRINT
30 PRINT "SORROW"
Line 10 makes the
computer print JOY. Line 20 makes the computer print a blank
empty line, underneath JOY. Altogether, the computer will print:
JOY
SORROW
Indenting
Suppose you want the
computer to print this letter:
DEAR JOAN,
THANK YOU FOR THE BEAUTIFUL
NECKTIE. JUST ONE PROBLEM--
I DON'T WEAR NECKTIES!
LOVE,
FRED-THE-HIPPIE
This program prints it:
10 PRINT "DEAR JOAN,"
20 PRINT " THANK YOU FOR THE BEAUTIFUL"
30 PRINT "NECKTIE. JUST ONE PROBLEM--"
40 PRINT "I DON'T WEAR NECKTIES!"
50 PRINT " LOVE,"
60 PRINT " FRED-THE-HIPPIE"
In the program, each
line contains two quotation marks. To make the computer indent a
line, put blank spaces after the first quotation mark.
Spaces after numbers
Try typing this command:
PRINT -3;"IS MY FAVORITE NUMBER"
Whenever the computer prints a number, it prints a blank space
afterwards; so the computer will print a blank space after -3,
like this:
-3 IS MY FAVORITE NUMBER
space
Spaces before numbers
This command tells what to put in your coffee:
PRINT 7;"DO";"NUTS"
The computer prints 7 and DO and NUTS. Since 7 is a number, the
computer prints a blank space after the 7. The computer prints
another blank space before every number that's positive; so the
computer prints another blank space before the 7, like this:
7 DONUTS
spaces
Hey, if you're feeling cool, maybe this command expresses your
feelings:
PRINT "THE TEMPERATURE IS";4+25;"DEGREES"
The computer prints THE TEMPERATURE IS, then 4+25 (which is 29),
then DEGREES. Since 29 is a positive number, the computer prints
a blank space before and after the 29:
THE TEMPERATURE IS 29 DEGREES
spaces
Use this command if you're even colder:
PRINT "THE TEMPERATURE IS";4-25;"DEGREES"
The computer prints THE TEMPERATURE IS, then 4-25 (which is -21),
then DEGREES. Since -21 is a number, the computer prints a space
after it; but since -21 is not positive, the computer does not
print a space before it. The computer prints:
THE TEMPERATURE IS-21 DEGREES
no space space
Yuk! That looks ugly! It would look prettier if there were a
space before the -21. To insert a space, put the space inside
quotation marks:
PRINT "THE TEMPERATURE IS ";4-25;"DEGREES"
inserted space, before the
quotation mark
Then the computer will print:
THE TEMPERATURE IS -21 DEGREES
inserted space
Multiple calculations
By using semicolons, you can make the computer do many
calculations at once.
For example, this command makes the computer do 6+2, 6-2, 6*2,
and 6/2, all at once:
PRINT 6+2;6-2;6*2;6/2
That makes the computer print the four answers:
8 4 12 3
The computer prints spaces between the answers, because the
computer prints a space after every number (and an additional
space before every number that's positive).
Clear the screen
If you press the CLEAR key (which is on the Walter keyboard),
the computer erases the entire screen, so that the screen becomes
cleared, empty, blank.
Though pressing the CLEAR key erases the screen, it does not
erase the computer's memory. The computer still remembers what
program you were working on. The only way to erase the computer's
memory is to type the word NEW.
So whenever you want to
begin a new program, type the word NEW, which erases the previous
program; pressing the CLEAR key is not sufficient.
You can begin your
program with this line:
10 CLS
It makes the computer automatically CLear the Screen, before
doing the rest of the program.
For example, run this
program:
10 CLS
20 PRINT "I'M SITTING ON TOP OF THE WORLD"
Line 10 makes the computer CLear the Screen. Since the screen's
been cleared, line 20 prints this message at the top of the
screen:
I'M SITTING ON TOP OF THE WORLD
On older computers, the
CLEAR key is missing and the CLS command doesn't work. To find
out about your computer, see the ``Versions of BASIC'' appendix.
Print on paper
You've learned how to
make the computer print messages, poems, and business letters on
your screen. But if you want to mail those messages and letters
to your friends, you face this problem: the post office refuses
to mail your screen! And it's hard to stuff your screen into an
envelope! So to mail the computer's wise words to your friends,
you must tell the computer to print on paper instead of on the
screen.
To make the computer
print on paper, attach the computer to a printer, which handles
paper and looks like a typewriter without a keyboard.
You can control the
printer in four ways.
The PRINT key The
simplest way is to press the PRINT key, which is on the Walter
keyboard. Pressing the PRINT key makes the printer dump onto
paper a snapshot of everything that's on the screen. The snapshot
on paper is called a screen dump.
The ECHO key A fancier
way to print on paper is to press the ECHO key, which is also on
the Walter keyboard. Pressing the ECHO key turns your entire
computer system into a gigantic typewriter. After you press the
ECHO key, whatever you or the computer type onto the screen will
also be echoed onto the paper. For example, if you press the ECHO
key and then type the word LOVE, the word LOVE will appear on
both the screen and the paper. The printer will continue to echo
everything that's typed, until you tell the printer to stop
echoing. To tell the printer to stop, just press the ECHO key
again.
The LPRINT command If
you type LPRINT instead of PRINT, the computer will print on
paper instead of on the screen. For example, to make the computer
print I LOVE YOU on paper, type this:
LPRINT "I LOVE YOU"
The LLIST command To
list your program on paper, instead of on the screen, type an
extra L in front of the word LIST, like this:
LLIST
Differences Although
modern computers include a PRINT key and an ECHO key and
understand LPRINT and LLIST, older computers do not. To find out
about your computer, read the ``Versions of BASIC'' appendix.
GOING & STOPPING
You can tell the computer to go and stop.
GO TO
This program makes the computer go wild:
10 PRINT "CAT"
20 PRINT "DOG"
30 GO TO 10
Line 10 makes the computer print CAT. Line 20 makes it print
DOG.
Line 30 makes it go back to line 10 again, so it prints CAT
again. Then it prints DOG again, and comes to line 30 again,
which makes it go back to line 10 again, print CAT and DOG again,
and then jump back again. . . .
The computer will print the words CAT and DOG again and again,
like this:
CAT
DOG
CAT
DOG
CAT
DOG
CAT
DOG
CAT
DOG
etc.
The computer will try to print the words CAT and DOG again and
again, forever.
Try running that program; you'll have fun watching the computer
go crazy. The computer will print the words CAT and DOG on every
line of your screen; and even when all the lines of your screen
have been used, the computer still doesn't stop: all the CATs and
DOGs rise to the top of the screen and finally off the screen, to
make way for new CATs and DOGs to appear at the bottom of the
screen. You'll see a blur, as thousands of CATs and DOGs go
flying up the screen.
If you told the computer to use the printer (which prints on
paper), you'll see reams of paper spewing out, saying CAT and
DOG. Soon your whole room will be buried under piles of paper
saying CAT and DOG thousands of times!
To stop this madness, you must give
the computer some kind of ``jolt'' that will put it out of its
misery. And now we come to the controversial part of this book.
To put the computer out of its misery, you must give an abortion.
To give the abortion, press the BREAK key. That makes the
computer stop running your program; it will abort your program
and say OK. Then you can give any command, such as LIST.
(The BREAK key is on the Walter
keyboard. To find out about your computer's keyboard, check the
``Versions of BASIC'' appendix.)
Here's a picture of that program:
start
10 PRINT "CAT"
20 PRINT "DOG"
30 GO TO 10
The computer follows the arrows, which make it go round and round
in a loop. Since the computer will try to go round and round the
loop forever, the loop is called infinite. The only way to stop
an infinite loop is to abort it.
In that program, you typed just
three lines; but since the third line said to GO TO the
beginning, the computer does an infinite loop. By saying GO TO,
you can make the computer do an infinite amount of work. Moral:
the computer can turn a finite amount of human energy into an
infinite amount of good. Putting it another way: the computer can
multiply your abilities by infinity.
Love
Let's spread love throughout the
world! Type this program, which makes the computer multiply your
love:
10 PRINT "LOVE"
20 GO TO 10
Even though you typed the word LOVE
just once, the computer will print it many times, like this:
LOVE
LOVE
LOVE
LOVE
LOVE
etc.
The computer will print repeatedly, until you abort its love.
For more lovely fun, put a semicolon
after LOVE, like this:
10 PRINT "LOVE";
20 GO TO 10
The semicolon makes the computer print LOVE next to LOVE:
LOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVEL
OVELOVELOVELOVE
LOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVEL
OVELOVELOVELOVE
LOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVEL
OVELOVELOVELOVE
etc.
If you put a space after LOVE, like
this ___
10 PRINT "LOVE ";
20 GO TO 10
the computer will put a space after each LOVE:
LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE
LOVE LOVE LOVE
LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE
LOVE LOVE LOVE
LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE LOVE
LOVE LOVE LOVE
etc.
Instead of making the computer print
LOVE, try making it print an ad (such as BUY JOE'S MEAT), or
political slogan (such as STOP POLLUTION), or your name, or
something else you feel emotional about. Like a dog, the computer
imitates its master's personality. If your computer acts ``cold
and heartless'', it's because you are!
Smelly poetry
Suppose you want to send this poem to all your friends:
I'M HAVING TROUBLE
WITH MY NOSE.
THE ONLY THING IT DOES IS:
BLOWS!
Just run this program:
10 PRINT "I'M HAVING TROUBLE"
20 PRINT "WITH MY NOSE."
30 PRINT "THE ONLY THING IT DOES IS:"
40 PRINT "BLOWS!"
50 PRINT
60 GO TO 10
Lines 10-40 print the poem. Line 50 prints a blank line at the
end of the poem. Line 60 makes the computer do all that
repeatedly, so the computer will print:
I'M HAVING TROUBLE
WITH MY NOSE.
THE ONLY THING IT DOES IS:
BLOWS!
I'M HAVING TROUBLE
WITH MY NOSE.
THE ONLY THING IT DOES IS:
BLOWS!
I'M HAVING TROUBLE
WITH MY NOSE.
THE ONLY THING IT DOES IS:
BLOWS!
etc.
The computer will print infinitely many copies of the poem ___
unless you abort it. If you activate the computer's printer, the
copies will be printed on paper, so you can mail them to your
nosy friends.
Life as an infinite loop
A program that makes the computer do the same thing again and
again forever is an infinite loop.
Some humans act just like computers. Those humans do the same
thing again and again. Every morning they GO TO work, and every
evening they GO TO home. GO TO work, GO TO home, GO TO work, GO
TO home, . . . Their lives are sheer drudgery. They're caught in
an infinite loop.
Go to your bathroom, get your bottle of shampoo, and look at
the instructions on the back. A typical bottle has three
instructions:
Lather.
Rinse.
Repeat.
Those instructions say to lather, then rinse, then repeat ___
which means to lather again, then rinse again, then repeat again
___ which means to lather again, then rinse again, then repeat
again. . . . If you follow those instructions, you'll never
finish washing your hair! The instructions are an infinite loop!
The instructions are a program: they program you to use lots of
shampoo! That's how infinite loops help sell shampoo.
Infinite pause
This program tortures
the computer:
10 GO TO 10
Line 10 tells the
computer to go to line 10, which tells the computer to go to line
10, which tells the computer to go to line 10. . . . The computer
will do line 10 again and again, forever. So the computer will
spend the rest of its life just mumbling to itself. That program
has turned your beautiful computer into a bumbling, mumbling
idiot.
The ``10 GO TO 10'' is
called an infinite pause, because it makes the computer pause
from doing all useful work, forever.
If you're mean enough to
give your computer that program, please abort it! Otherwise, a
member of the Association for Prevention of Cruelty to Computers
will stick pins in your voodoo doll.
To make the program even
meaner, insert line 5, like this:
5 CLS
10 GO TO 10
Line 5 makes the computer CLear the Screen, so that the whole
screen becomes blank. And line 10 renders the computer completely
useless: the computer will ignore anything you type, since the
computer is busy doing ``10 GO TO 10'' forever. Now you have a
computer whose screen is permanently blank and that ignores
anything you type on the keyboard! To get really mean, walk up to
your best friend's computer, type that program, type the word
RUN, and watch your friend suffer when the screen and keyboard
both stop working! (And see how long your friend remains your
``friend''!) The only way your (ex-)friend can fix the computer
is to give an abortion or turn off the power.
(Remember: most
computers understand CLS, but yours might not. To find out about
your computer, check the ``Versions of BASIC'' appendix.)
To elaborate on that
program, add line 7, like this:
5 CLS
7 PRINT "I REFUSE TO WORK FOR YOU"
10 GO TO 10
When you RUN that program, the screen goes blank, then the
computer says it refuses to work for you, and then line 10 does
indeed make the computer refuse to work for you! The computer
will refuse all your work ___ it'll just mumble to itself ___
until you give it an abortion.
Now that I've turned you
into a computer saboteur, the whole world will worry that you'll
kidnap computers, hold them hostage, and threaten to turn them
into ``10 GO TO 10'' idiots! You can even cause a moral dilemma
for people who are against abortions and are even more against
``pulling the plug''. Is God against abortions for computers?
How to skip ahead
Did you ever dream about having a picnic in the woods? This
program expresses that dream:
10 PRINT "LET'S MUNCH"
20 PRINT "SANDWICHES UNDER"
30 PRINT "THE TREES"
It makes the computer print:
LET'S MUNCH
SANDWICHES UNDER
THE TREES
Let's turn that dream into a nightmare, in which we all become
giant termites. To do that, insert the shaded line:
10 PRINT "LET'S MUNCH"
15 GO TO 30
20 PRINT "SANDWICHES UNDER"
30 PRINT "THE TREES"
The computer does line 10, which prints LET'S MUNCH. Then it does
line 15, which makes it skip ahead to line 30, which prints THE
TREES. So altogether, the computer prints:
LET'S MUNCH
THE TREES
In that program, line 15 made the computer skip ahead to line
30 and skip over line 20.
END
To make the computer skip the bottom part of your program, say
END:
10 PRINT "SHE SMELLS"
20 END
30 PRINT "OF PERFUME"
The computer will print SHE SMELLS and then end, without printing
OF PERFUME.
After the computer ends, it'll say OK. Then you can type any
command, such as LIST (to see the program again) or RUN (to make
the computer print SHE SMELLS again) or NEW (to create a new
program).
STOP
Instead of saying END, you can say STOP, like this:
10 PRINT "SHE SMELLS"
20 STOP
30 PRINT "OF PERFUME"
The computer will print SHE SMELLS and then stop, without
printing OF PERFUME.
When the computer encounters the word STOP, it prints a message
telling you which line said STOP.
For example, when the computer stops because line 20 said STOP,
the computer prints ``STOPPED AT LINE 20'' or ``BREAK IN LINE
20'' or ``BREAK IN 20''. (The exact wording depends on which
computer you bought.)
So if you type ___
10 PRINT "SHE SMELLS"
20 STOP
30 PRINT "OF PERFUME"
and then type RUN, the computer will print something like this:
SHE SMELLS
BREAK IN 20
DISKS VERSUS TAPES
While you're working
with a program, the computer keeps the program in its
random-access memory (RAM). In a typical microcomputer, the RAM
handles just one program at a time. If you want the computer to
remember several programs, you can put one program in the RAM but
must put the remaining programs on a disk or tape instead.
Save on disk
Here's how to use a
disk. (I'll explain tape later.)
Before typing your
program, make sure the computer and disk have both been properly
prepared. To find out how to prepare your computer and disk, read
the ``Versions of BASIC'' appendix.
Then type your program.
For example, try typing this program:
NEW
10 PRINT "MY DAD"
20 PRINT "IS GLAD"
Underneath the program,
try typing this:
SAVE "JOE"
That makes the computer copy the program from the RAM to the
disk, and name the program JOE.
Copying from the RAM to
the disk is called ``saving'' the program. SAVE ``JOE'' means:
save the program and name it ``JOE''.
Altogether, here's what
you typed:
NEW
10 PRINT "MY DAD"
20 PRINT "IS GLAD"
SAVE "JOE"
(On your computer, the
procedure might be slightly different: check the ``Versions of
BASIC'' appendix.)
To make sure the
computer copied the program onto the disk, type this word:
FILES
The computer will print the names of all the programs on the
disk, and one of the names it will print is JOE.
The program is now in
two places: the program's original version is still in the RAM,
and a copy of it is on the disk. The copy is called JOE.
If you type NEW, you'll
erase the RAM, and so the program won't be in the RAM anymore.
But although the program's original version is no longer in the
RAM, the copy (which is called JOE) remains on the disk. To prove
JOE's still on the disk, type the word FILES again, which makes
the computer say again that JOE's still on the disk.
How to choose a name
If you don't like the name JOE, choose a different name
instead. For example, to create a program named SUE, type a
program and then say:
SAVE "SUE"
The name of your program can be ``JOE'' or ``SUE'' or any other
brief string of letters and digits. For example, it can be
``LOVER'' or ``POEM4U''.
Pick a name that reminds you of the program's purpose. For
example, if the program prints a bill to a customer, call the
program ``BILL''; if the program plays chess, call the program
``CHESS''; if it gives a quiz, call it ``QUIZ''; if it tutors a
human about the elements of sex, call it ``SEX''; if it tutors a
human about advanced sex, call it ``SEX2''.
Load from disk
You've seen that the word SAVE makes the computer copy a
program from the RAM to the disk. The opposite of the word SAVE
is the word LOAD: it makes the computer copy a program from the
disk to the RAM.
For example, if you want the computer to copy JOE from the disk
to the RAM, type this:
LOAD "JOE"
It tells the computer to load JOE into the RAM (from the disk).
After typing that, the program's in the RAM again. To prove it's
in the RAM, type the word LIST, so that the computer will LIST
the program that's in the RAM; or type the word RUN, which makes
the computer RUN the program that's in the RAM.
So if JOE's on a disk, you can run it by first copying it to
the RAM and then saying RUN, like this:
LOAD "JOE"
RUN
Instead of typing that pair of lines, you can type this single
line, which is a short-cut:
RUN "JOE"
That makes the computer automatically load JOE (from the disk to
the RAM) and run it.
Edit the disk
Suppose you don't like the version of JOE that's on the disk.
Here's how to revise JOE.
First, copy JOE from the disk to the RAM, by saying:
LOAD "JOE"
Then examine the program, by typing:
LIST
Then revise the program, by retyping some of its lines or
adding new ones.
When you've finished revising the program's version that's in
the RAM, copy the revisions from the RAM to disk, by saying
again:
SAVE "JOE"
That command makes the computer copy the revised version of JOE
onto the disk (and erases the previous version of JOE from the
disk).
If JOE's on the disk and you want to change its name to FRED,
type this:
RENAME "JOE" TO "FRED"
Protect yourself
Suppose you're creating
a program named TOM, which is so long that it takes you several
hours to type. You'll be upset if, after several hours of typing,
your town suddenly has a blackout that makes the computer forget
what you typed.
To protect yourself
against such a calamity, type SAVE ``TOM'' every fifteen minutes.
Then if your town has a blackout, you'll lose just a few minutes
of work; the rest of your work will have already been saved on a
disk.
Typing SAVE ``TOM''
every fifteen minutes protects you against blackouts and also
against ``computer malfunction'' and any careless errors you
might make.
Each time you say SAVE
``TOM'', the computer puts a copy of the program onto the disk
and saves that copy permanently ___ or at least until you type
SAVE ``TOM'' again fifteen minutes later. Even if your RAM is
erased (by a blackout or computer malfunction or your
carelessness), TOM will still be on the disk.
Space on the disk
If you invent many
programs and put them on the disk, the disk will someday become
full. If you try to put more programs onto the disk than the disk
can hold, the computer will gripe by printing a message such as:
DISK FULL
Someday, you'll decide
you no longer have any use for JOE, and that JOE's just wasting
space on your disk. To erase JOE from the disk, type this:
KILL "JOE"
Tapes
If you can't afford a
disk drive, try using cassette tapes instead, which serve the
same purpose as disks but are cheaper and slower. (To find out
whether your computer accepts cassette tapes, check the
``Versions of BASIC'' appendix.)
Here's how to put a
program onto a cassette tape. First, type the program. Then put a
tape into the tape recorder; to do that, you might have to press
the recorder's EJECT button.
Rewind the tape, by
pressing the recorder's REWIND button. When the tape is rewound,
press the recorder's STOP button.
If the recorder has a
counter, press the counter's button, so the counter becomes zero.
Press the recorder's
FAST FORWARD button, until you get to a part of the tape that's
blank and good. (The very beginning of the tape is bad. Wait
until the counter gets to at least 10.) Then press the STOP
button.
If the recorder has a
counter, notice the counter's number, and write it onto the
tape's label, by using a pencil.
To find out what to do
next, read the ``Versions of BASIC'' appendix, which explains the
peculiarities of your computer!